﻿using System;
using Cysharp.Threading.Tasks;
using uMVP.Dialog;
using UnityEngine;

namespace ExceptionModule
{
    public class LogToUI : ICustomLogHandler
    {
        private readonly Func<IDevelopModelMessageDialog> _modelMessageDialogFactory;
        private IDevelopModelMessageDialog _modelMessageDialog;

        public LogToUI(Func<IDevelopModelMessageDialog> modelMessageDialogFactory)
        {
            _modelMessageDialogFactory = modelMessageDialogFactory;
        }

        public void HandleLog(string logString, string stackTrace, LogType type)
        {
            if (type != LogType.Exception)
                return;

            var log = $"{DateTime.Now} {type}:{logString}\n{stackTrace}";
            _modelMessageDialog = _modelMessageDialogFactory();
            _modelMessageDialog.OpenAsync(log).Forget();
        }

        public void Dispose()
        {
            _modelMessageDialog?.CloseAsync().Forget();
        }
    }
}